package com.example.click.huanxin.presenter.impl;

import android.text.TextUtils;
import android.widget.Toast;

import com.example.click.huanxin.entity.User;
import com.example.click.huanxin.presenter.LoginPresenter;
import com.example.click.huanxin.presenter.callback.RegisterListener;
import com.example.click.huanxin.ui.RegisterLoginActivity;
import com.hyphenate.chat.EMClient;
import com.hyphenate.exceptions.HyphenateException;

import java.util.ArrayList;

import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;

/**
 * Created by Chen qin lang on 2016/12/29.
 * 邮箱 :   619766577@qq.com
 */
public class RegisterImpl implements LoginPresenter{

    public RegisterLoginActivity registerLoginActivity=new RegisterLoginActivity();
    @Override
    public void regist(String username, String pwd, RegisterListener registerListener) {
        final ArrayList<String> errorLists=new ArrayList<>();
        //注册失败会抛出HyphenateException
        User user=new User();
        if (TextUtils.isEmpty(username)&&TextUtils.isEmpty(pwd)){
            return;
        }
        try {

            user.setName(username);
            user.setPassword(pwd);
            user.save(new SaveListener<String>() {
                @Override
                public void done(String s, BmobException e) {
                    if (e==null){
                        registerLoginActivity.showSuccess();
                    }else {
                        errorLists.add("bmob错误");
                    }
                }
            });
            EMClient.getInstance().createAccount(username, pwd);//同步方法
            registerListener.onSuccess();
        } catch (HyphenateException e) {
            e.printStackTrace();
            errorLists.add("huanxin错误");
            user.delete();
            registerListener.onError(errorLists);
        }
    }

    @Override
    public void Error() {

    }
}
